package org.jetlinks.community.mstn.entity;

import com.alibaba.fastjson.JSON;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.jetlinks.core.metadata.PropertyMetadata;
import org.jetlinks.core.metadata.SimplePropertyMetadata;
import org.jetlinks.core.metadata.types.DateTimeType;
import org.jetlinks.core.metadata.types.StringType;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class CustomFormLogEntity {

    @Schema(description = "日志ID")
    private String id;

    @Schema(description = "创建时间")
    private long createTime;

    @Schema(description = "表单内容")
    private Object content;

    @Schema(description = "表单配置信息")
    private Object configuration;

    @Schema(description = "消息类型")
    private String type;

    @Schema(description = "机构id")
    private String orgId;

    @Schema(description = "表单Id")
    private String formId;

    @Schema(description = "数据时间")
    private long timestamp;


    public Map<String, Object> toSimpleMap() {
        Map<String, Object> result = (Map) JSON.toJSON(this);
        result.put("type", getType());
        if (getContent() instanceof String) {
            result.put("content", getContent());
        } else {
            result.put("content", JSON.toJSONString(getContent()));
        }
        if (getConfiguration() instanceof String) {
            result.put("configuration", getConfiguration());
        } else {
            result.put("configuration", JSON.toJSONString(getConfiguration()));
        }
        return result;
    }

    public static List<PropertyMetadata> getProperties(){
        List<PropertyMetadata> properties = new ArrayList<>();

        SimplePropertyMetadata timestamp = new SimplePropertyMetadata();
        timestamp.setId("timestamp");
        timestamp.setValueType(new DateTimeType());
        properties.add(timestamp);

        SimplePropertyMetadata createTime = new SimplePropertyMetadata();
        createTime.setId("createTime");
        createTime.setValueType(new DateTimeType());
        properties.add(createTime);

        SimplePropertyMetadata orgId = new SimplePropertyMetadata();
        orgId.setId("orgId");
        orgId.setValueType(new StringType());
        properties.add(orgId);

        SimplePropertyMetadata type = new SimplePropertyMetadata();
        type.setId("type");
        type.setValueType(new StringType());
        properties.add(type);

        SimplePropertyMetadata id = new SimplePropertyMetadata();
        id.setId("id");
        id.setValueType(new StringType());
        properties.add(id);

        return properties;
    }
}
